package com.tyhy.utils;

import java.util.HashMap;
import java.util.Map;

import com.tyhy.enums.ReturnCodeEnum;
//TODO 待兼容springboot抛出的异常格式
//{
//    "timestamp": "2020-03-28T05:56:58.526+0000",
//    "status": 400,
//    "error": "Bad Request",
//    "message": "JSON parse error: xx",
//    "path": "/ecp/send/sendEcp"
//}
public class R extends HashMap<String, Object> {
	private static final long serialVersionUID = 1L;

	public R() {
		put("code", ReturnCodeEnum.CODE_E00500.getCode());
		put("msg", "操作成功");
		put("data", null);

	}

	public static R error() {
		return error(ReturnCodeEnum.CODE_E00500.getCode(), ReturnCodeEnum.CODE_E00500.getValue());
	}

	public static R error(String msg) {
		return error(ReturnCodeEnum.CODE_E00500.getCode(), msg);
	}

	public static R error(String code, String msg) {
		R r = new R();
		r.put("code", code);
		r.put("msg", msg);
		return r;
	}

	public static R error(ReturnCodeEnum ReturnCode) {
		return error(ReturnCode.getCode(), ReturnCode.getValue());
	}

	public static R ok(String code, String msg,String data) {
		R r = new R();
		r.put("code", code);
		r.put("msg", msg);
		r.put("data", data);
		return r;
	}
	public static R okData(String data) {
		
		R r = new R();
		r.put("code", ReturnCodeEnum.CODE_N00000.getCode());
		r.put("msg", "操作成功");
		r.put("data", data);
		
		return r;
	}

	public static R ok(String msg) {
		R r = new R();
		r.put("code", ReturnCodeEnum.CODE_N00000.getCode());
		r.put("msg", msg);
		return r;
	}

	public static R ok(Map<String, Object> map) {
		R r = new R();
		r.put("code", ReturnCodeEnum.CODE_N00000.getCode());
		r.putAll(map);
		return r;
	}

	public static R ok() {
		return new R().put("code", ReturnCodeEnum.CODE_N00000.getCode());
	}

	@Override
	public R put(String key, Object value) {
		super.put(key, value);
		return this;
	}

	public String getCode() {
		return this.get("code") == null ? null : this.get("code").toString();
	}

	public String getMsg() {
		Object msg = this.get("msg");
		if (msg == null) {
			return "网络不稳定,服务处理失败";
		}
		return this.get("msg").toString();
	}

	public String getData() {
		Object data = this.get("data");
		if (data == null) {
			return null;
		}
		return data.toString();
	}

}
